package com.cloud.thread;

import com.cloud.commons.constants.ConsumeConstant;
import com.cloud.commons.utils.CloudCopyUtils;
import com.cloud.commons.utils.DateUtils;
import com.cloud.dao.assets.AssetsDao;
import com.cloud.dao.assetsdetail.AssetsDetailDao;
import com.cloud.dao.bookkeeping.BookkeepingDao;
import com.cloud.dao.wxuser.WxUserDao;
import com.cloud.dao.wxusersubscribe.WxUserSubscribeDao;
import com.cloud.model.entity.assets.Assets;
import com.cloud.model.entity.assetsdetail.AssetsDetail;
import com.cloud.model.entity.wxuser.WxUser;
import com.cloud.model.entity.wxusersubscribe.WxUserSubscribe;
import com.cloud.model.view.assets.AssetsView;
import com.cloud.model.view.assetsdetail.AssetsDetailView;
import com.cloud.model.view.bookkeeping.BookkeepingView;
import com.cloud.service.assets.AssetsService;
import com.cloud.service.assetsdetail.AssetsDetailService;
import com.cloud.service.wxtemplatemessage.WxTemplateMessageService;
import com.mysql.cj.util.TestUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.ObjectUtils;

import javax.annotation.PostConstruct;
import java.util.Map;

/**
 * 子线程 - 负责统计总消费记录，月消费记录
 */
@Component
public class AssetsDetailThread extends Thread {


    private Long userId;

    private Long consumeTime;


    public static AssetsDetailThread assetsDetailThread;

    @Autowired
    private AssetsDetailDao assetsDetailDao;

    @Autowired
    private AssetsDetailService assetsDetailService;

    @Autowired
    private BookkeepingDao bookkeepingDao;

    @Autowired
    private AssetsService assetsService;

    @Autowired
    private AssetsDao assetsDao;

    /**
     * 普通类调用bean 方法初始化
     */
    @PostConstruct
    public void init() {
        assetsDetailThread = this;
        assetsDetailThread.assetsDetailDao = this.assetsDetailDao;
        assetsDetailThread.bookkeepingDao = this.bookkeepingDao;
        assetsDetailThread.assetsDetailService = this.assetsDetailService;
        assetsDetailThread.assetsService = this.assetsService;
        assetsDetailThread.assetsDao = this.assetsDao;

    }

    public AssetsDetailThread(Long userId,Long consumeTime) {
        this.userId = userId;
        this.consumeTime = consumeTime;
    }

    public AssetsDetailThread() {}
    public void run() {
//        System.out.println("我这边调用了多线程负责统计总消费记录，月消费记录接口");
        Long startTime = DateUtils.getCurrentMonthFirstDay(consumeTime);
        Long endTime = DateUtils.getCurrentMonthLastDay(consumeTime);
        AssetsDetail assetsDetail = assetsDetailThread.assetsDetailDao.findByUserIdAndNowFirstMonth(userId, startTime);
        if (ObjectUtils.isEmpty(assetsDetail)) {
            // 获取不到数据，重新新建数据信息
            this.saveAssetsAndAssetsDetail(userId);
            assetsDetail = assetsDetailThread.assetsDetailDao.findByUserIdAndNowFirstMonth(userId, startTime);
        }
        Map<String, Long> bookkeepingMap = assetsDetailThread.bookkeepingDao.sunByUserIdAndConsumeTimeBetween(userId, startTime, endTime);

        Long income = !ObjectUtils.isEmpty(bookkeepingMap.get("income")) ? Long.valueOf(String.valueOf(bookkeepingMap.get("income"))) : 0L;
        Long expenditure = !ObjectUtils.isEmpty(bookkeepingMap.get("expenditure")) ? Long.valueOf(String.valueOf(bookkeepingMap.get("expenditure"))) : 0L;
        Long money = income - expenditure;

        // 更新月收入
        AssetsDetailView assetsDetailView = new AssetsDetailView();
        assetsDetailView.setMonthTotalIncome(income);
        assetsDetailView.setMonthTotalExpenditure(expenditure);
        assetsDetailView.setMonthTotalMoney(money);
        assetsDetailView.setId(assetsDetail.getId());
        assetsDetailThread.assetsDetailService.updateEntity(assetsDetailView);

        // 更新总收入
        Map<String, Long> assetsDetailMap = assetsDetailThread.assetsDetailDao.sunMoneyByAssetsId(assetsDetail.getAssetsId());
        Long totalIncome = Long.valueOf(String.valueOf(assetsDetailMap.get("income")));
        Long totalExpenditure = Long.valueOf(String.valueOf(assetsDetailMap.get("expenditure")));
        Long totalMoney = Long.valueOf(String.valueOf(assetsDetailMap.get("money")));
        AssetsView assetsView = new AssetsView();
        assetsView.setId(assetsDetail.getAssetsId());
        assetsView.setTotalIncome(totalIncome);
        assetsView.setTotalExpenditure(totalExpenditure);
        assetsView.setTotalMoney(totalMoney);
        assetsDetailThread.assetsService.updateEntity(assetsView);

//        System.out.println("子线程 - 统计消费记录数据结束");
    }

    private void saveAssetsAndAssetsDetail(Long userId) {
        Assets assets = assetsDetailThread.assetsDao.findByUserId(userId);
        AssetsView assetsView = new AssetsView();
        if (!ObjectUtils.isEmpty(assets)) {
            // 存在则复制
            CloudCopyUtils.copyBean(assets, assetsView);
        } else {
            // 不存在新建数据
            assetsView = createAssetsView(userId);
        }


        Long monthFirstDay = DateUtils.getCurrentMonthFirstDay(System.currentTimeMillis());
        AssetsDetail assetsDetail = assetsDetailDao.findByAssetsIdAndNowFirstMonth(assetsView.getId(), monthFirstDay);
        if (ObjectUtils.isEmpty(assetsDetail)) {
            // 不存在新建数据
            createAssetsDetailView(assetsView.getId(), userId);

        }
    }

    private AssetsView createAssetsView(Long userId) {
        AssetsView assetsView = new AssetsView();
        assetsView.setUserId(userId);
        assetsView.setTotalMoney(ConsumeConstant.MONRY_INIT);
        assetsView.setTotalIncome(ConsumeConstant.MONRY_INIT);
        assetsView.setTotalExpenditure(ConsumeConstant.MONRY_INIT);
        assetsView = assetsDetailThread.assetsService.saveEntity(assetsView);
        return assetsView;
    }


    private void createAssetsDetailView(Long assetsId, Long userId) {
        AssetsDetailView assetsDetailView = new AssetsDetailView();
        assetsDetailView.setAssetsId(assetsId);
        assetsDetailView.setUserId(userId);
        assetsDetailView.setMonthTotalExpenditure(ConsumeConstant.MONRY_INIT);
        assetsDetailView.setMonthTotalMoney(ConsumeConstant.MONRY_INIT);
        assetsDetailView.setMonthTotalIncome(ConsumeConstant.MONRY_INIT);
        assetsDetailView.setNowMonth(DateUtils.getYearAndMonth(System.currentTimeMillis()));
        assetsDetailView.setNowFirstMonth(DateUtils.getCurrentMonthFirstDay(System.currentTimeMillis()));
        assetsDetailThread.assetsDetailService.saveEntity(assetsDetailView);
    }
}
